package com.ruoyi.web.controller.erp;

import com.ruoyi.system.domain.Customer;
import com.ruoyi.system.domain.SalesOrder;
import com.ruoyi.system.service.ICustomerService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/erp/customer")
public class CustomerController {

    @Resource
    ICustomerService iCustomerService;

    /**
     * 查询客户列表
     *
     * @param customer 客户信息
     * @return 客户集合
     */
    @PostMapping("/selectCustomerList")
    public List<Customer> selectCustomerList(Customer customer){
        List<Customer> list=iCustomerService.selectCustomerList(customer);
        return  list;
    }

    /**
     * 查询客户详细信息
     *
     * @param customerId 客户ID
     * @return 客户信息
     */
    @PostMapping("/selectCustomerById")
    public Customer selectCustomerById(Long customerId){
        Customer customer=iCustomerService.selectCustomerById(customerId);
        return customer;
    }

    /**
     * 查询客户关联的销售单
     *
     * @param customerId 客户ID
     * @return 销售单列表
     */
    @PostMapping("/selectSalesOrdersByCustomerId")
    public List<SalesOrder> selectSalesOrdersByCustomerId(Long customerId) {
        List<SalesOrder> list=iCustomerService.selectSalesOrdersByCustomerId(customerId);
        return list;
    }

    /**
     * 新增客户
     *
     * @param customer 客户信息
     * @return 结果
     */
    @PostMapping("/insertCustomer")
    public Map insertCustomer(Customer customer){
        Map map=new HashMap();
        int rows=iCustomerService.insertCustomer(customer);
        if (rows>0){
            String msg="添加成功";
            map.put("msg",msg);
        }else {
            String msg="添加失败";
            map.put("msg",msg);
        }
        return map;
    }

    /**
     * 修改客户
     *
     * @param customer 客户信息
     * @return 结果
     */
    @PostMapping("/updateCustomer")
    public Map updateCustomer(Customer customer){
        Map map=new HashMap();
        int rows=iCustomerService.updateCustomer(customer);
        if (rows>0){
            String msg="修改成功";
            map.put("msg",msg);
        }else {
            String msg="修改失败";
            map.put("msg",msg);
        }
        return map;
    }

    /**
     * 删除客户信息
     *
     * @param customerId 客户ID
     * @return 结果
     */
    @PostMapping("/deleteCustomerById")
    public Map deleteCustomerById(Long customerId){
        Map map=new HashMap();
        int rows=iCustomerService.deleteCustomerById(customerId);
        if (rows>0){
            String msg="修改成功";
            map.put("msg",msg);
        }else {
            String msg="修改失败";
            map.put("msg",msg);
        }
        return map;
    }

    /**
     * 批量删除客户信息
     *
     * @param customerIds 需要删除的客户ID
     * @return 结果
     */
    @PostMapping("/deleteCustomerByIds")
    public Map deleteCustomerByIds(Long[] customerIds){
        Map map=new HashMap();
        int rows=iCustomerService.deleteCustomerByIds(customerIds);
        if (rows==customerIds.length){
            String msg="修改成功";
            map.put("msg",msg);
        }else {
            String msg="修改失败";
            map.put("msg",msg);
        }
        return map;
    }
}
